<template>
    <div class="subtemp">
        <span><input type="button" value="-" @click="subtract"></span>
        <span><input type="text" v-model="count"></span>
        <span><input type="button" value="+" @click="add"></span>
    </div>
</template>

<script>
    export default {
        props:['initCount','goodsid'],
        data() {
            return {
                count:1,
                resObj:{goodsid:0,type:'add'}
            }
        },
        created(){
          this.count = this.initCount;
        },
        methods:{
            add() {
                this.count++;
                this.sendData("add");
            },
            subtract() {
                this.count--;
//                确保最小值为1
                if(this.count<1) {
                    this.count = 1;
                    return;
                }else {

                    this.sendData("subtract");
                }
            },
            sendData(type) {
                this.resObj.type = type;
                this.resObj.goodsid = this.goodsid;
                this.$emit('sendData',this.resObj);
            }
        }
    }
</script>

<style scoped>
    .subtemp input {
        width: 30px;
        height: 30px;
    }
    .subtemp input[type="text"] {
        width: 40px;
        padding: 0;
        text-align: center;
    }
</style>